//
//  JKNumberChangeAnimationView.swift
//  ZhiHuDaily-Swift
//
//  Created by albert on 2018/3/11.
//  Copyright © 2018年 安永博. All rights reserved.
//

import UIKit

class JKNumberChangeAnimationView: UIView {
    
    /** 类方法 初始化 */
    public class func viewWithOriginalNumber(originalNumber: Int) -> JKNumberChangeAnimationView {
        
        let view = JKNumberChangeAnimationView()
        
        view.originalNumber = originalNumber
        
        return view
    }
    
    public var originalNumber = 0 {
        
        didSet{
            
            if (self.numViews.count > 0) {
                return
            }
            
            let originalNumStr = "\(self.originalNumber)"
            let changedNumStr = "\(self.originalNumber + 1)"
            
            let originalLength = originalNumStr.count
            let changedLength = changedNumStr.count
            
            let width: CGFloat = 10
            let height: CGFloat = self.height
            let Y: CGFloat = 0
            var X: CGFloat = 0
            
            if (changedLength > originalLength) {
                
                let lb1 = UILabel()
                lb1.textColor = UIColor.white
                lb1.frame = self.bounds
                lb1.text = originalNumStr
                lb1.textAlignment = NSTextAlignment.center
                lb1.font = UIFont.systemFont(ofSize: fontSize)
                self.addSubview(lb1)
                self.numViews.append(lb1)
                
                let lb2 = UILabel()
                lb2.textColor = UIColor.white
                lb2.frame = self.bounds
                lb2.y = height
                lb2.text = changedNumStr
                lb2.textAlignment = NSTextAlignment.center
                lb2.font = UIFont.systemFont(ofSize: fontSize)
                self.addSubview(lb2)
                self.numViews.append(lb2)
                
                return
            }
            
            for i in 0 ..< changedLength {
                
                X = (self.width - width * (CGFloat(changedLength))) * 0.5 + (CGFloat(i)) * width
                
                // 顶部
                let originalC = (originalNumStr as NSString).substring(with: NSMakeRange(i, 1))
                
                // 底部
                let changedC = (changedNumStr as NSString).substring(with: NSMakeRange(i, 1))
                
                let numV = UIView()
                numV.frame = CGRectMake(X, Y, width, height * 2)
                self.addSubview(numV)
                self.numViews.append(numV)
                
                // 顶部label
                let topLb = UILabel()
                topLb.textColor = UIColor.white
                topLb.frame = CGRectMake(0, 0, width, height)
                topLb.text = originalC
                topLb.textAlignment = NSTextAlignment.center
                topLb.font = UIFont.systemFont(ofSize: fontSize)
                numV.addSubview(topLb)
                
                
                
                
                if originalC == changedC {
                    
                    topLb.frame = CGRectMake(X, Y, width, height)
                    self.addSubview(topLb)
                    
                    continue
                }
                
                // 底部label
                let bottomLb = UILabel()
                bottomLb.textColor = UIColor.white
                bottomLb.frame = CGRectMake(0, height, width, height)
                bottomLb.text = changedC
                bottomLb.textAlignment = NSTextAlignment.center
                bottomLb.font = UIFont.systemFont(ofSize: fontSize)
                numV.addSubview(bottomLb)
            }
        }
    }
    
    // MARK: - 私有属性
    
    /** 字体大小 */
    private let fontSize: CGFloat = 16
    
    /** 动画时间 */
    private let animationDuration: CGFloat = 0.3
    
    /** 要做动画的数字view */
    private lazy var numViews = [UIView]()

    // MARK: - 初始化
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        initialization()
    }
    
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        
        initialization()
    }
    
    private func initialization() {
        
        self.backgroundColor = JKColor(0, 132, 208).withAlphaComponent(0.7)
        self.clipsToBounds = true
        self.alpha = 0
    }
    
    /** 开始动画 */
    public func beginAnimationWithTransform(transform: CGAffineTransform, complete: (() -> Void)?) {
        
        // 第1个动画
        UIView.animate(withDuration: TimeInterval(animationDuration), animations: {
            
            self.transform = transform
            self.alpha = 1
            
        }) { (_) in
            
            // 第2个动画
            UIView.animate(withDuration: TimeInterval(self.animationDuration * 2), animations: {
                
                for vv in self.numViews {
                    
                    vv.transform = CGAffineTransformMakeTranslation(0, -self.height)
                }
                
            }) { (_) in
                
                // 第3个动画
                UIView.animate(withDuration: TimeInterval(self.animationDuration), delay: TimeInterval(self.animationDuration * 0.5), options: UIViewAnimationOptions(rawValue: 0), animations: {
                    
                    self.transform = CGAffineTransform.identity
                    self.alpha = 0
                    
                }, completion: { (_) in
                    
                    self.isHidden = true
                    
                    for vv in self.numViews {
                        
                        vv.transform = CGAffineTransform.identity
                    }
                    
                    if complete != nil {
                        
                        complete!()
                    }
                })
            }
        }
    }
}
